package com.gxj.study.shopdemo.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.google.gson.reflect.TypeToken;
import com.gxj.study.shopdemo.Contants;
import com.gxj.study.shopdemo.R;
import com.gxj.study.shopdemo.WareDetailActivity;
import com.gxj.study.shopdemo.adapter.BaseAdapter;
import com.gxj.study.shopdemo.adapter.decoration.DividerItemDecortion;
import com.gxj.study.shopdemo.adapter.HWAdapter;
import com.gxj.study.shopdemo.bean.Page;
import com.gxj.study.shopdemo.bean.Wares;
import com.gxj.study.shopdemo.http.OkHttpHelper;
import com.gxj.study.shopdemo.http.SpotsCallBack;
import com.gxj.study.shopdemo.utils.Pager;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class HotFragment extends BaseFragment implements Pager.OnPageListener<Wares>{
    private HWAdapter mAdatper;

    @ViewInject(R.id.recyclerView)
    private RecyclerView mRecyclerView;

    @ViewInject(R.id.refresh_view)
    private MaterialRefreshLayout mRefreshLayout;


    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        return inflater.inflate(R.layout.fragment_hot,container,false);
    }

    @Override
    public void init() {
        Pager pager = Pager.newBuilder()
                .setUrl(Contants.API.WARES_HOT)
                .setLoadMore(true)
                .setOnPageListener(this)
                .setPageSize(20)
                .setmRefreshLayout(mRefreshLayout)
                .build(getActivity(),new TypeToken<Page<Wares>>(){}.getType());
        pager.request();
    }

    @Override
    public void load(List<Wares> datas, int totalPage, int totalCount) {
        mAdatper = new HWAdapter(getActivity(),datas);

        mAdatper.setmOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                Wares wares = mAdatper.getItem(position);

                Intent intent = new Intent(getActivity(), WareDetailActivity.class);

                intent.putExtra(Contants.WARE,wares);
                startActivity(intent);


            }
        });
        mRecyclerView.setAdapter(mAdatper);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void refresh(List<Wares> datas, int totalPage, int totalCount) {
        mAdatper.refreshData(datas);

        mRecyclerView.scrollToPosition(0);
    }

    @Override
    public void loadMore(List<Wares> datas, int totalPage, int totalCount) {
        mAdatper.loadMoreData(datas);
        mRecyclerView.scrollToPosition(mAdatper.getDatas().size());
    }
}